<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2006 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

class CMS_Post extends Pluf_Model
{
    function init()
    {
        $this->_a['verbose'] = __('post');
        $this->_a['table'] = 'cms_posts';
        $this->_a['model'] = 'CMS_Post';
        $this->_a['cols'] = array(
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  'blank' => true, 
                                  ),
                            'website' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'CMS_Website',
                                  'blank' => false,
                                  'verbose' => __('website'),
                                  ),
                            'title' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 200,
                                  'verbose' => __('title'),
                                  ),
                            'content' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Text',
                                  'blank' => false,
                                  'verbose' => __('content'),
                                  ),
                            'content_format' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 10,
                                  'default' => 'text/html',
                                  'verbose' => __('format of the content'),
                                  ),
                            'extended' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Text',
                                  'blank' => true,
                                  'verbose' => __('extended content'),
                                  ),
                            'extended_format' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 10,
                                  'default' => 'text/html',
                                  'verbose' => __('format of the extended content'),
                                  ),
                            'slug' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 150,
                                  'verbose' => __('slug'),
                                  'help_text' => __('URL to access the resource.'),
                                  'unique' => true,
                                  ),
                            'author' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'Pluf_User',
                                  'blank' => false,
                                  'verbose' => __('author'),
                                  ),
                            'post_type' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => true,
                                  'size' => 10,
                                  'verbose' => __('type'),
                                  'help_text' => __('You can give different types to your post to classify them a little bit more than with tags. For example you can create a type "citation" for small citations and then you can select a special template based on this type.'),
                                  ),
                            'tags' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Manytomany', 
                                  'blank' => true,
                                  'model' => 'CMS_Tag',
                                  ),
                            'comments' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer', 
                                  'blank' => false,
                                  'verbose' => __('comments'),
                                  'choices' => array(__('Open') => 1,
                                                     __('Must be validated') => 2,
                                                     __('Closed') => 3),
                                  ),
                            'status' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'default' => 1,
                                  'blank' => false,
                                  'verbose' => __('status'),
                                  'choices' => array(
                                                     __('On line') => 1,
                                                     __('In edition') => 4,
                                                     __('Off line') => 2,
                                                     ),
                                  ),
                            'creation_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('creation date'),
                                  ),
                            'modif_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('modification date'),
                                  ),
                            'publication_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('publication date'),
                                  ),
                            );
        $this->_a['idx'] = array(                           
                                 'slug_idx' =>
                                 array(
                                       'col' => 'slug',
                                       'type' => 'unique',
                                       ),
                                 'website_idx' =>
                                 array(
                                       'col' => 'website',
                                       'type' => 'normal',
                                       ),
                                                            );
        $this->_a['views'] = array();
    }

    /**
     * String representation of the user.
     */
    function __toString()
    {
        return $this->title;
    }

    /**
     * Set the date times.
     *
     */
    function preSave()
    {
        if (!($this->id > 0)) {
            $this->creation_dtime = gmdate('Y-m-d H:i:s');
        }
        if ('' == $this->publication_dtime) {
            $this->publication_dtime = gmdate('Y-m-d H:i:s');
        }
        if ('' == $this->slug) {
            $slug = mb_ereg_replace('\s+', '-', $this->title);
            $slug = mb_ereg_replace('[^A-Za-z0-9]', '-', $slug);
            $slug = mb_strtolower($slug, 'UTF-8');
            $this->slug = gmdate('/Y/m/d/').$slug;
        }
        $this->modif_dtime = gmdate('Y-m-d H:i:s');
    }

    /**
     * Get the comments for the current post.
     */
    function get_comment_list($params=array())
    {
        if ($this->id == '') {
            return new ArrayObject();
        }
        return CMS_Comment::retrieve($this, $params);
    }
}
